const Vue = require('vue')
const server = require('express')()
// 2.
const fs = require('fs')
const renderer = require('vue-server-renderer').createRenderer({
  template: fs.readFileSync('./index.template.html', 'utf-8')
})


server.get('*', (req, res) => {

  const app = new Vue({
    data: {
      url: req.url
    },
    template: `<div>访问的url: {{url}}</div>`
  })

  renderer.renderToString(app, (err, html) => {
    if (err) {
      res.status(500).end('Internal Server Error')
      return
    }


    // 设置编码
    // res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' })
    // 1. 拼接字符串
    // res.end(`
    //   <!DOCTYPE html>
    //   <html lang="en">
    //     <head><title>Hello</title></head>
    //     <body>${html}</body>
    //   </html>
    // `)
    
    // 2. 导入模板
    res.end(html)

  })
})


server.listen(8000, () => { console.log('listen 8000')})